<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Config.php 3986 2010-07-25 16:32:46Z huuphuoc $
 * @since		2.0.0
 */

class Tomato_Config 
{
	const KEY = 'Tomato_Config_';
	
	/**
	 * Get application config object
	 * 
	 * @return Zend_Config
	 */
	public static function getConfig() 
	{
		try{
			if (!Zend_Registry::isRegistered("JAM-GATE")){
				$JAM_GATE	= new stdClass();
				$JAM_GATE->jam_id	= "jam0001";
				Zend_Registry::set("JAM-GATE",$JAM_GATE);			
			}		
						
			$jam_id 	= Zend_Registry::get("JAM-GATE")->jam_id;
			
			if($jam_id){// có mã số jam_id lấy file config của jam_id đó								
				$hostIni 	  = 'application_'.$jam_id.'.ini';				
				$keyHost 	  = 'JAM-GATE-'.$jam_id.'-config';
				if (!Zend_Registry::isRegistered($keyHost)) {
					$hostConfig    = TOMATO_APP_DIR . DS . 'config' . DS . $hostIni;				
					if(file_exists($hostConfig)){
						$config = new Zend_Config_Ini($hostConfig);					
						Zend_Registry::set($keyHost, $config);								
					}else throw new Exception("File Config không tồn tại.");									
				}
				return Zend_Registry::get($keyHost);				
			}else{// không có jam_id lấy file config của jam-gate								
				$defaultConfig = TOMATO_APP_DIR . DS . 'config' . DS . 'application.ini';
				$key_default  = 'JAM-GATE-config';
				if (!Zend_Registry::isRegistered($key_default)) {
					if(file_exists($defaultConfig)){
						$config = new Zend_Config_Ini($defaultConfig);						
						Zend_Registry::set($key_default, $config);					
					}
				}
				return Zend_Registry::get($key_default);				
			}
		}catch(Exception $ex){
			echo $ex->getMessage();
			die;
		}
	}
}
